import { _decorator, Animation, Color, Component, director, Label, Node, Sprite, tween, Vec3 } from 'cc';
import { aa } from '../frame/FrameCore';
import { aaEvent } from '../frame/common/Enums';
const { ccclass, property } = _decorator;

 const color =  new Color(255,255,255,255);

@ccclass('OpponentCtrl')
export class OpponentCtrl extends Component {
    @property(Sprite) head:Sprite = null;
    @property(Animation) anim: Animation = null;
    @property(Label) num: Label = null;

    protected onEnable(): void {
        director.on(aaEvent.angry,this.showEmotion,this);
        this.angry = 0;
    }
    protected onDisable(): void {
        director.off(aaEvent.angry,this.showEmotion,this);
        this.anim.stop();
        this.num.node.scale = Vec3.ZERO;
    }
    set angry(v){
        this._angry = v;
        const c = 255*(1-v)
        color.b = c;
        color.g = c;
        this.head.color = color;
    }
    get angry(){
        return this._angry;
    }


    private _angry = 0;
    init(time=0) {

        if(time>0){
            this.scheduleOnce(()=>{
                this.clear();
            },time);
        }

    }
    showEmotion(num){
        this.num.string = "-"+num;
        this.anim.play();
        const self = this.getComponent(OpponentCtrl)
        tween(self).to(0.2, { angry:1}).delay(0.4).to(0.1, { angry:0 }).start();
    }

    clear(){
        aa.res.put(this.node);
    }

  
}

